import org.w3c.dom.Node;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-02-20
 * Time: 11:29
 */
public class test1 {
    /**
     * LeetCode 429  N叉树的层序遍历
     */

    // BFS
    // 拿出队头元素之后将其孩子添加到队列中
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ret = new ArrayList<>();
        if(root==null){
            return ret;
        }
        Queue<Node> q = new LinkedList<>();
        q.add(root);
        while(!q.isEmpty()){
            int size = q.size();
            List<Integer> tmp = new ArrayList<>();
            for(int i=0;i<size;i++){
                Node t = q.poll();
                tmp.add(t.val);
                for(Node child: t.children){
                    if(child!=null){
                        q.add(child);
                    }
                }
            }
            ret.add(tmp);
        }
        return ret;
    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    };
}
